package com.zgroup.controllers.admin

import com.zgroup.common.CheckUtils
import com.zgroup.common.WebUtils
import com.zgroup.domain.InfoRelease
import com.zgroup.domain.Picture

class PictureController extends AdminDispatchController {
  def scaffoldService
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
  def beforeInterceptor = [
          action: this.&auth
  ]

  def auth() {
    return super.auth()
  }

  def index = {
    redirect(action: "list", params: params)
  }

  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [pictureInstanceList: Picture.list(params), pictureInstanceTotal: Picture.count()]
  }

  def create = {
    def pictureInstance = new Picture()
    pictureInstance.properties = params
    return [pictureInstance: pictureInstance]
  }

  def save = {
    def pictureInstance = new Picture(params)

    def uploadedFile = request.getFile("file")
    if (!uploadedFile.empty) {
      def fileName = WebUtils.generateUuidHex() + WebUtils.getFileExt(uploadedFile.originalFilename)
//          println "Class: ${uploadedFile.class}"
//          println "Name: ${uploadedFile.name}"
//          println "OriginalFileName: ${uploadedFile.originalFilename}"
//          println "Size: ${uploadedFile.size}"
//          println "ContentType: ${uploadedFile.contentType}"
      pictureInstance.fileName = fileName
      def webRootDir = servletContext.getRealPath("/")
      def userDir = new File(webRootDir, InfoRelease.UPLOAD_FILE_PATH)
      userDir.mkdirs()
      uploadedFile.transferTo(new File(userDir, fileName))

      // ImageUtils.resize(InfoRelease.UPLOAD_FILE_PATH + fileName,InfoRelease.UPLOAD_THUMBNAIL_PATH + fileName,100,96);
    }

    pictureInstance.dateCreated = new Date();


    if (pictureInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'picture.label', default: 'Picture'), pictureInstance.id])}"
      redirect(action: "show", id: pictureInstance.id)
    }
    else {
      render(view: "create", model: [pictureInstance: pictureInstance])
    }
  }

  def show = {
    def pictureInstance = Picture.get(params.id)
    if (!pictureInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'picture.label', default: 'Picture'), params.id])}"
      redirect(action: "list")
    }
    else {
      [pictureInstance: pictureInstance]
    }
  }

  def edit = {
    def pictureInstance = Picture.get(params.id)
    if (!pictureInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'picture.label', default: 'Picture'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [pictureInstance: pictureInstance]
    }
  }

  def update = {
    def pictureInstance = Picture.get(params.id)
    if (pictureInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (pictureInstance.version > version) {

          pictureInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'picture.label', default: 'Picture')] as Object[], "Another user has updated this Picture while you were editing")
          render(view: "edit", model: [pictureInstance: pictureInstance])
          return
        }
      }
      pictureInstance.properties = params
      if (!pictureInstance.hasErrors() && pictureInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'picture.label', default: 'Picture'), pictureInstance.id])}"
        redirect(action: "show", id: pictureInstance.id)
      }
      else {
        render(view: "edit", model: [pictureInstance: pictureInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'picture.label', default: 'Picture'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def pictureInstance = Picture.get(params.id)
    def view = params.view
    if (pictureInstance) {
      try {
        if (!CheckUtils.isEmpty(pictureInstance.fileName)) {
          //def webRootDir = grailsApplication.config.grails.imagesRealPath
          def webRootDir = servletContext.getRealPath("/")
          def userDir = new File(webRootDir, InfoRelease.UPLOAD_FILE_PATH)
          File oldHeadImg = new File(userDir, pictureInstance.fileName)
          //println "file = " + (oldHeadImg.getPath());
          oldHeadImg.delete()
        }

        pictureInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'picture.label', default: 'Picture'), params.id])}"
        if (!CheckUtils.isEmpty(view)) {
          render true
          return;
        }
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'picture.label', default: 'Picture'), params.id])}"
        if (!CheckUtils.isEmpty(view)) {
          render false
          return;
        }
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'picture.label', default: 'Picture'), params.id])}"
      if (!CheckUtils.isEmpty(view)) {
        render false
        return;
      }
      redirect(action: "list")
    }
  }
}
